using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridManager : SingletonpatternMonoBase<GridManager>
{

    public Grid[,] grids;
    
   
    public Vector2Int gridNum;
    public Vector2 gridSize;
    


    void Awake()
    {
        grids = new Grid[gridNum.x, gridNum.y];
        foreach (Transform t in transform)
        {
            int i = t.GetComponent<Grid>().row;
            int j = t.GetComponent<Grid>().col;
            grids[i, j] = t.GetComponent<Grid>();

        }

    }


    public bool IsInBox(Vector2Int _gridNum)
    {
        return _gridNum.x >= 0 && _gridNum.x <= gridNum.x && _gridNum.y >= 0 && _gridNum.y <= gridNum.y;
    }

    public bool IsCanArrive(Vector2Int _gridNum)
    {
        return IsInBox(_gridNum) && 
               grids[_gridNum.x, _gridNum.y].IsCanMove &&
               grids[_gridNum.x, _gridNum.y].gridCharacter == null;
    }
    
    public Vector2Int WorldToGridPosition(Vector2 worldPosition)
    {
        int x = Mathf.FloorToInt(worldPosition.x / gridSize.x);
        int y = Mathf.FloorToInt(worldPosition.y / gridSize.y);
        return new Vector2Int(x, y);
    }
    public Vector2 GridToWorldPosition(Vector2Int gridPosition)
    {
        float x = (gridPosition.x * gridSize.x) + (gridSize.x / 2);
        float y = (gridPosition.y * gridSize.y) + (gridSize.y / 2);
        return new Vector2(x, y);
    }
    public Vector2 GridToWorldPosition(int gridPositionX, int gridPositionY)
    {
        float x = (gridPositionX * gridSize.x) + (gridSize.x / 2);
        float y = (gridPositionY * gridSize.y) + (gridSize.y / 2);
        return new Vector2(x, y);
    }
  
   /* public static Vector2 GetClosestDirection(Vector2 from, Vector2 to, DirectionMode mode)
    {
        Vector2 direction = to - from;
        float angle = Vector2.SignedAngle(Vector2.up, direction);

        switch (mode)
        {
            case DirectionMode.Cross:
                if (angle > -45 && angle <= 45)
                {
                    return Vector2.up;
                }
                else if (angle > 45 && angle <= 135)
                {
                    return Vector2.left;
                }
                else if (angle > -135 && angle <= -45)
                {
                    return Vector2.right;
                }
                else
                {
                    return Vector2.down;
                }

            

            case DirectionMode.EightDirections:
                if (angle > -22.5 && angle <= 22.5)
                {
                    return Vector2.up;
                }
                else if (angle > 22.5 && angle <= 67.5)
                {
                    return new Vector2(-1, 1);
                }
                else if (angle > 67.5 && angle <= 112.5)
                {
                    return Vector2.left;
                }
                else if (angle > 112.5 && angle <= 157.5)
                {
                    return new Vector2(-1, -1);
                }
                else if (angle > -157.5 && angle <= -112.5)
                {
                    return new Vector2(1, -1);
                }
                else if (angle > -112.5 && angle <= -67.5)
                {
                    return Vector2.right;
                }
                else if (angle > -67.5 && angle <= -22.5)
                {
                    return new Vector2(1, 1);
                }
                else
                {
                    return Vector2.down;
                }
        }

        return Vector2.zero; // 默认返回，理论上不应该执行到这里
    }
    public static Vector2 GetRandomDirection(int choice)
    {
        int randomIndex;
        switch (choice)
        {
            case 0:
                randomIndex = Random.Range(0, 4);

                switch (randomIndex)
                {
                    case 0:
                        return Vector2.up;
                    case 1:
                        return Vector2.down;
                    case 2:
                        return Vector2.right;
                    case 3:
                        return Vector2.left;
                    default:
                        return Vector2.zero;
                }
            case 1:
                randomIndex = Random.Range(0, 8);

                switch (randomIndex)
                {
                    case 0:
                        return Vector2.up;
                    case 1:
                        return Vector2.down;
                    case 2:
                        return Vector2.right;
                    case 3:
                        return Vector2.left;
                    case 4:
                        return new Vector2(1, 1);
                    case 5:
                        return new Vector2(-1, 1);
                    case 6:
                        return new Vector2(-1, -1);
                    case 7:
                        return new Vector2(1, -1);
                    default:
                        return Vector2.zero;
                }
            default:
                return Vector2.zero;
        }

    }
    public static int GetManhattanDistance(Vector2 a, Vector2 b)//哈曼
    {
        return Mathf.Abs((int)a.x - (int)b.x) + Mathf.Abs((int)a.y - (int)b.y);
    }
    public static int ChebyshevDistance(Vector2 a, Vector2 b)//切比洛夫
    {
        return Mathf.Max(Mathf.Abs((int)a.x - (int)b.x), Mathf.Abs((int)a.y - (int)b.y));
    }*/
}
